import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/get.dart';

import 'emoji_panel.dart';

class SendCommentWidget extends StatefulWidget {
  Function sendMsgFunc;

  SendCommentWidget({Key key,
    this.sendMsgFunc}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>SendCommentState();


}

class SendCommentState extends State<SendCommentWidget> {
  RxBool emojiSelected = false.obs;

  TextEditingController commentController = TextEditingController();

  @override
  void dispose() {
    if(commentController!=null)commentController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets,
      duration: Duration(milliseconds: 100),
      child: Container(
        width: PxUtil.instance.screenWidth,
        padding: EdgeInsets.only(
          bottom: PxUtil.instance.px_10,
          top: PxUtil.instance.px_10,
        ),
        color: ColorUtil.instance.color_304070,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                SizedBox(width: PxUtil.instance.px_15,),
                Expanded(
                    child: Container(
                      height: PxUtil.instance.px_36,
                      alignment: Alignment.centerLeft,
                      decoration: BoxDecoration(
                          color: ColorUtil.instance.color_767F99,
                          borderRadius:
                          BorderRadius.circular(PxUtil.instance.px_18)),
                      child: Row(
                        children: [
                          Expanded(child: TextField(
                            style: TextStyle(
                                color: ColorUtil.instance.color_white
                            ),
                            enabled: true,
                            textAlign: TextAlign.start,
                            maxLength: 200,
                            maxLines: 1,
                            textInputAction: TextInputAction.done,
                            keyboardType: TextInputType.text,
                            autofocus: true,
                            controller: commentController,
                            // onChanged: (value)=>((onCommentChanged==null)?((){}):(onCommentChanged(value))),
                            cursorColor: ColorUtil.instance.color_white,
                            decoration: InputDecoration(
                                contentPadding: EdgeInsets.only(
                                  left: PxUtil.instance.px_15,
                                  right: PxUtil.instance.px_15,
                                  bottom: PxUtil.instance.px_0,
                                ),
                                isDense: true,
                                counterText: "",
                                focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                        color: ColorUtil
                                            .instance.color_transparent,
                                        width: 1)),
                                enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                        color: ColorUtil
                                            .instance.color_transparent,
                                        width: 1))),
                          )),
                          Obx(() => InkWellButton(
                            onPressed: ()=>emojiSelect(),
                            child: Container(
                              margin: EdgeInsets.only(
                                left: PxUtil.instance.px_10,
                                right: PxUtil.instance.px_10,
                              ),
                              child: ImagesUtil.loadAssetImage(
                                  DrawableUtil.instance.icon_chat_emoji,
                                  width: PxUtil.instance.px_24,
                                  color: (emojiSelected.value??false)?Colors.blue:Colors.white60
                              ),
                            ),
                          ))
                        ],
                      ),
                    )),
                Container(
                  margin: EdgeInsets.only(
                    left: PxUtil.instance.px_15,
                    right: PxUtil.instance.px_15,
                  ),
                  child: GestureDetector(
                    onTap: (){
                      var comments = commentController.text;
                      if(comments.isEmpty){
                        ToastUtil.toast(StringUtil.instance.Please_input_your_idea);
                        return;
                      }
                      if(widget.sendMsgFunc!=null)widget.sendMsgFunc(comments);
                    },
                    child: ImagesUtil.loadAssetImage(
                        DrawableUtil.instance.icon_send,
                        width: PxUtil.instance.px_25,
                        fit: BoxFit.fitWidth
                    ),
                  ),
                )
              ],
            ),
            //底部栏
            Obx(() => AnimatedContainer(
              duration: const Duration(milliseconds: 300),
              height: isOpenButtonUi()?PxUtil.instance.px_197:PxUtil.instance.px_0,
              child: _getPanel(),
            ))
          ],
        ),
      ),
    );
  }
  emojiSelect() {
    emojiSelected.value=!(emojiSelected.value??false);
  }
  Widget _getPanel() {

    if (emojiSelected.value??false) {
      return EmojiPanel(
        onEmojiSelected: (emoji) {
          final text = commentController.text;
          final textFinal =  "$text$emoji";
          commentController.text = textFinal;
          commentController.selection=TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream,
              offset: textFinal.length));
          // inputText = model.commentController.text;
          // Future.delayed(Duration(milliseconds: 20), () {
          //   model.commentScrollController
          //       .jumpTo(model.commentScrollController.position.maxScrollExtent);
          // });
        },
        onEmojiDelete: () {
          String originText = commentController.text;
          var text = originText.characters.skipLast(1);
          commentController.text = "$text";
          commentController.selection=TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream,
              offset: commentController.text.length));
          // inputText = model.commentController.text;
        },
      );
    }
    return Container();
  }
  bool isOpenButtonUi() {
    return emojiSelected.value;
  }
}
